﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using Training.Library.DomainObject.Administration.UserManagement;
using Training.Web.Support;
using Training.Library.DataAccess.Administration.Catalog;
using Training.Library.DomainObject.Administration.Catalog;

namespace Training.Web.Lists.Administration.Catalog
{

    public partial class InqueryPesertaList : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            List<Karyawan> list = KaryawanDal.GetList();
            
            //if (base.CabangID != null)
            //{
                Cabang objCabang = CabangDal.GetById(base.CabangID);
                if (objCabang != null)
                {
                    if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                    {
                        //parameters = parameters == string.Empty ? " WHERE Pelatihan.CabangID=" + base.CabangID : parameters + " AND Pelatihan.CabangID=" + base.CabangID;
                        var query = list.Where(x => x.CabangID == base.CabangID).OrderBy(x => x.NIK);
                        list = query.ToList();
                    }
                    else
                    {
                        var query = list.OrderBy(x => x.NIK);
                        list = query.ToList();
                    }
                }
            //}

            
            
          
            ddlNIK.Items.Clear();

            list.ForEach(delegate(Karyawan obj)
            {
                ddlNIK.Items.Add(new ListItem(obj.NIK + " - " + obj.NamaKaryawan, obj.NIK.ToString()));
            });

            ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

            if (!Page.IsPostBack)
                GetPrivilege();

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnAdd').hide();$('#btnEdit').hide();$('#btnDelete').hide();", true);

                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnAdd').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnEdit').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Delete):
                        ClientScript.RegisterStartupScript(typeof(Page), "delete", "$('#btnDelete').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}